var prefix = "/psPerson/manage";
$().ready(function () {
    $("#province").val("");
    $("#city").val("");
    validateRule();
});
$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function onTypeChange() {
    validateRule();
}

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/save",
        data: $('#carForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                if (data.msg) {
                    parent.layer.alert(data.msg)
                } else {
                    window.location.href = "/";
                }
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#carForm").validate({
        rules: {
            username: {
                required: true
            },
            code: {
                required: true
            },
            mobile: {
                required: true
            },
            qq: {
                required: true
            },
            address: {
                required: true
            },
            storeroom: {
                required: true
            },
            storeroomAddress: {
                required: true
            },
            province: {
                required: true
            },
            city: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "司机姓名不能为空"
            },
            code: {
                required: icon + "编号不能为空"
            },
            mobile: {
                required: icon + "手机号码不能为空"
            },
            qq: {
                required: icon + "QQ/微信不能为空"
            },
            address: {
                required: icon + "联系地址不能为空"
            },
            storeroom: {
                required: icon + "仓库名不能为空"
            },
            storeroomAddress: {
                required: icon + "仓库地址不能为空"
            },
            province: {
                required: icon + "配送省不能为空"
            },
            city: {
                required: icon + "配送市不能为空"
            }
        }
    })
}

function chooseWarehouse() {
    layer.open({
        id: "choose_warehouse",
        type: 2,
        title: "选择仓库",
        maxmin: true,
        shadeClose: true,
        area: ['90%', '80%'],
        content: "/warehouse/manage/chooseWarehouse"
    })
}

function loadWarehouseInfo(id, name, address) {
    $('#storeroomId').val(id);
    $('#storeroom').val(name);
    $('#storeroomAddress').val(address);
}


/**
 * 选择线路配送门店
 */
function chooseAddXiaoQus() {
    var province = $('#province').val();
    var city = $('#city').val();
    var mids = $('#mids').val();
    var xqs = $('#xqs').val();
    if (!province) {
        layer.msg("请选择配送省份");
        return;
    }
    if (!city) {
        layer.msg("请选择配送城市");
        return;
    }
    layer.open({
        id: "choose_chooseXiaoQus",
        type: 2,
        title: "门店选择",
        maxmin: true,
        shadeClose: true,
        area: ['60%', '98%'],
        content: "/carxq/manage/chooseAddXiaoQu?province=" + province + "&city=" + city + "&mids="+ mids + "&xqs=" + xqs
    })
}

function loadXiaoQuInfo(xqNames,mids) {
    $('#xiaoqu').val(xqNames);
    $('#xqs').val(xqNames);
    $('#mids').val(mids);
}

function clearXiaoqu() {
    $('#xiaoqu').val("");
}
